<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\User as DealerUserModel;
use app\api\model\Withdraw as WithdrawModel;
use app\common\model\UserCapital;

/**
 * 分销商提现
 * Class Withdraw
 * @package app\api\controller\user\dealer
 */
class Withdraw extends Controller
{
    /* @var \app\api\model\User $user */
    private $user;

    private $dealer;
    private $setting;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // 用户信息
        $this->user = $this->getUser();
        $withdrawn_d = \app\common\model\Withdraw::where(['user_id'=> $this->user['user_id'],'apply_status'=>['in','10,20']])->sum('money');//待提现佣金
        $this->user['balance'] = $this->user['balance'] - $withdrawn_d;
        // 分销商用户信息
        $this->dealer = DealerUserModel::detail($this->user['user_id']);
    }

    /**
     * 提交提现申请
     * @param $data
     * @return array
     * @throws \app\common\exception\BaseException
     */
    public function submit($data)
    {
        $formData = json_decode(htmlspecialchars_decode($data), true);
        $model = new WithdrawModel;
        if ($model->submit($this->dealer, $formData)) {
            return $this->renderSuccess([], '提现申请已提交成功，请等待审核');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

    /**
     * 分销商提现明细
     * @param int $status
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($status = -1)
    {
        $model = new WithdrawModel;
//        $start = $this->request->post("start", '');
//        $end = $this->request->post("end", '');
        $where = [];
        $where1 = [];
        $date = $this->request->param("date", '');

        if($date){
            //$date_arr = explode('-',$date);
            $start = strtotime("$date".'-01');
            $end = date("Y-m-d", strtotime("+1 months", $start));
//            $start = $date_arr[0];
//            $end = $date_arr[1];
            if($start && $end){//between
                $start_time = $start;
                $end_time = strtotime($end);
                $where['create_time'] = ['between',"$start_time,$end_time"];
                $where1['create_time'] = ['between',"$start_time,$end_time"];
            }
        }else{
            $date = date("Y-m-01", time());
            $start = strtotime("$date".'-01');
            $end = date("Y-m-d", strtotime("+1 months", $start));
//            $start = $date_arr[0];
//            $end = $date_arr[1];
            if($start && $end){//between
                $start_time = $start;
                $end_time = strtotime($end);
                $where['create_time'] = ['between',"$start_time,$end_time"];
                $where1['create_time'] = ['between',"$start_time,$end_time"];
            }
        }

//        if($start && $end) {//between
//            $start_time = strtotime($start);
//            $end_time = strtotime($end);
//            $where['create_time'] = ['between',"$start_time,$end_time"];
//            $where1['create_time'] = ['between',"$start_time,$end_time"];
//        }else{
//            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
//            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//            $where['create_time'] = ['between',"$beginToday,$endToday"];
//        }
        $month = UserCapital::where(['user_id'=>$this->user['user_id'],'type'=>['in','1,2']])->where($where)->sum('money');//今日佣金
        $total = UserCapital::where(['user_id'=>$this->user['user_id'],'type'=>['in','1,2']])->sum('money');//今日佣金
        $month_withdrawal = $model->where(['user_id'=>$this->user['user_id'],'apply_status'=>['in','10,20,40']])->where($where)->sum('money');//本月提现
        $withdrawal = $model->where(['user_id'=>$this->user['user_id'],'apply_status'=>['in','10,20,40']])->sum('money');//今日佣金
        return $this->renderSuccess([
            // 提现明细列表
            'list' => $model->getList($this->user['user_id'], (int)$status,$where1),
            'month' => $month,//本月收入
            'month_withdrawal' => $month_withdrawal,//本月提现
            'total' =>  $this->user['balance'],//累计佣金金额
            'commission' => $this->user['balance']-$withdrawal,//佣金金额
            // 页面文字
            'words' => $this->setting['words']['values'],
        ]);
    }

}